##########################################
## "Voters get what they want"          ##
##########################################
## Heinrich, Kobayashi, Long            ##
##########################################

##          
## Probabilities of hypotheses
## Script 08
## June 29, 2017


## Load estimated models
########################
to_get <- c(list.files(path="output/analysis1_interaction/", pattern="Output", full.names=TRUE),
            list.files(path="output/analysis1_subset/", pattern="Output", full.names=TRUE))
eff <- vector("list", length(to_get))
for(i in 1:length(to_get))
{
  load(to_get[i])
  
  eff[[i]] <- output$SubstEff
  eff[[i]]$Model <- ifelse(grepl(pattern="interaction", x=to_get[i]) == TRUE, "Interaction", "Subset")
}
eff <- ldply(.data=eff, .fun=function(x) x)
eff <- ddply(.data=subset(eff, Hier == 0),
             .variables=c("Model", "NewsSet", "NewsType", "HierType"),
             .fun=function(x) data.frame(ProbNegative=x$ProbNegative,ProbMEChange=mean(x$ProbMEChange)))
eff$NewsType2 <- ifelse(eff$NewsType == "Leader", "NYT (Leader)", "NYT (violations)")
eff$Name <- paste0(eff$HierType, ", ", eff$Model, " model", "\n",
                   ifelse(grepl(x=eff$NewsSet, "40"), "Regular news cutoff", 
                                              "Restrictive news cutoff"), ", ",
                   ifelse(grepl(x=eff$NewsType, "Leader"), "Leader news", "Human rights news"))
eff <- melt(data=eff, measure.vars=c("ProbNegative", "ProbMEChange"),
            variable.name="ProbType")
eff$ProbType2 <- ifelse(eff$ProbType == "ProbNegative", "... negative at low hierarchy?", "... increasing with hierarchy?")
eff$ProbType2 <- factor(eff$ProbType2, levels=rev(c("... increasing with hierarchy?", "... negative at low hierarchy?")))
eff$IsMain <- factor(ifelse(grepl(x=eff$Name, "Subset"), 1, 0) * ifelse(grepl(x=eff$Name, "Leader"), 1, 0) * ifelse(grepl(x=eff$Name, "Regular"), 1, 0))


## Probabilities for support
## Large table
############################
eff$Name <- factor(eff$Name, levels=c(unique(subset(eff, IsMain == 0)$Name),
                                      unique(subset(eff, IsMain == 1)$Name)))
eff$IsHigh <- ifelse(grepl(x=eff$NewsSet, pattern="Top") == TRUE, "High news coverage", "Low news coverage")
g <- ggplot(data=eff, aes(x=Name, y=value, fill=IsMain))
g <- g + geom_hline(yintercept=c(.95, .99), size=.3)
g <- g + geom_hline(yintercept=1, size=.8)
g <- g + facet_grid(IsHigh ~ ProbType2)
g <- g + geom_point(size=4.1, colour="black", shape=21) + coord_flip()
g <- g + xlab("") + ylab("Posterior probability")
g <- g + scale_fill_manual(values=c("gray80", "black"), guide=FALSE)
g <- g + theme_bw() + ggtitle("Is the marginal effect of human rights abuses on foreign aid ...")
g <- g + theme(axis.text = element_text(size=rel(.625)),
               panel.grid.major.y = element_line(size=0.25, colour="grey80", 
                                                 linetype="dashed"),
               panel.grid.major.x = element_blank(),               
               panel.grid.minor = element_blank(),
               axis.ticks = element_blank(),
               strip.text = element_text(size=rel(1.0), hjust=0, vjust=1),
               strip.background = element_blank(),
               plot.title = element_text(size=rel(1.2), face="bold", hjust=0))
g
ggsave(file="output/figures/A1-Probabilities-Big.pdf", width=11, height=9.5)



## Probabilities for support
## Main table
############################
eff$ProbType2 <- ifelse(eff$ProbType == "ProbNegative", "... negative at low hierarchy?",
                        "...  increasing with hierarchy?")
eff$ProbType2 <- factor(eff$ProbType2, levels=rev(c("...  increasing with hierarchy?",
                                                    "... negative at low hierarchy?")))
eff <- subset(eff, IsMain == 1)
eff$IsHigh <- str_replace_all(string=eff$IsHigh, pattern=" news coverage", replacement="")
eff$IsHigh <- factor(eff$IsHigh, levels=rev(c("High", "Low")))
eff$HierType <- str_replace_all(string=eff$HierType, pattern=" h", replacement="\nh")
g <- ggplot(data=eff, aes(x=HierType, y=value, group=IsHigh, fill=IsHigh))
g <- g + geom_hline(yintercept=c(.95, .99), size=.2)
g <- g + geom_hline(yintercept=1, size=.8)
g <- g + facet_grid(. ~ ProbType2)
g <- g + geom_point(size=4.7, colour="black", shape=21, position=position_dodge(width=.2)) + coord_flip()
g <- g + xlab("") + ylab("Posterior probability")
g <- g + theme_bw() + ggtitle("Is the marginal effect of human rights abuses on foreign aid ...")
g <- g + scale_fill_manual(values=c("gray80", "black"), name="News coverage")
g <- g + theme(axis.text = element_text(size=rel(.83)),
               panel.grid.major.y = element_line(size=0.25, colour="grey80", 
                                                 linetype="dashed"),
               panel.grid.major.x = element_blank(),               
               panel.grid.minor = element_blank(),
               axis.ticks = element_blank(),
               strip.text = element_text(size=rel(1.0), hjust=0, vjust=1),
               strip.background = element_blank(),
               plot.title = element_text(size=rel(1.2), face="bold", hjust=0))
g
ggsave(file="output/figures/A1-Probabilities-Main.pdf", width=11, height=4)




